home *** CD-ROM | disk | FTP | other *** search
-
- /* ----- PROTOTYPE APPLICATION --- KIO ! --- 20.11.93 ----
- Copyright (C) 1993-1994 G.Woigk
-
- This file is part of the ZX LOADER program and it is free software
- See Application.c for details
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-
- 22.06.94: removed bug in handling required AppleEvent 'quit'
- */
-
- #include <AppleEvents.h>
- #include <GestaltEqu.h>
- #include <Menus.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include "kio.h"
- #include "mem.h"
- #include "application.h"
- #include "eventloop.h"
- #include "console.h"
-
-
- // ----- Standarddialogs -----------------------------------------------------
-
- short aboutID = 660; // 'ALRT' resourceID: DoAbout
- short helpID = 661; // 'ALRT' resourceID: DoHelp
- short alertID = 666; // 'ALRT' resourceID: DoAbort,DoAlert
- short warningID = 667; // 'ALRT' resourceID: DoWarning
- short messageID = 668; // 'ALRT' resourceID: DoMessage
- short confirmID = 669; // 'ALRT' resourceID: DoConfirm
- short requestID = 670; // 'ALRT' resourceID: DoRequest
-
- // ----- Event Tracking -------------------------------------------------------
-
- extern EventRecord event; // event returned by WaitNextEvent
- extern Boolean done; // break flag for wait next event loop
- extern long eventDelay;
- extern RgnHandle eventRegion;
- Boolean oApp = FALSE; // HandleINIT() already called?
- short eventMask = everyEvent; //
-
-
- // ----- prototypes ----------------------------------------------------------
- short DoBox ( short id, Str255 s, Str255 t, Str255 u );
- OSErr MyGotRequiredParams ( AppleEvent *theAppleEvent );
- pascal OSErr HandleOpenAPP(AppleEvent *eventIn, AppleEvent *eventOut, long refCon);
- pascal OSErr HandleQuitAPP(AppleEvent *eventIn, AppleEvent *eventOut, long refCon);
- OSErr OpenAppl (AppleEvent *eventIn, long how);
- pascal OSErr HandleOpenDOC(AppleEvent *eventIn, AppleEvent *eventOut, long refCon);
- pascal OSErr HandlePrintDOC(AppleEvent *eventIn, AppleEvent *eventOut, long refCon);
- InitToolBox();
- InitHighLevelEvents();
- HandleFileMenu(long itemNum);
- HandleFileMenu(long itemNum);
- HandleAppleMenu(long itemNum);
- HandleMenuSelection(long menuResult);
- HandleMouseDown();
- HandleEditMenu(long itemNum);
-
-
- /* ***** STANDARD DIALOGS ***************************************** */
-
- short DoBox ( short id, Str255 s, Str255 t, Str255 u )
- { long n;
- ParamText(s,t,u,0);
- n = Alert ( id, nil );
- if (n>0) return(n);
- if (id!=alertID) DoAbort("\pEine 'ALRT' Resource fehlt!","\p","\p");
- SysBeep(20);SysBeep(20);SysBeep(20);SysBeep(20);
- ExitToShell();
- }
-
- DoAlert ( Str255 s, Str255 t, Str255 u ) { DoBox (alertID, s,t,u); }
- DoWarning ( Str255 s, Str255 t, Str255 u ) { DoBox (warningID, s,t,u); }
- DoMessage ( Str255 s, Str255 t, Str255 u ) { DoBox (messageID, s,t,u); }
- short DoConfirm ( Str255 s, Str255 t, Str255 u ) { return DoBox ( confirmID, s,t,u ); }
- short DoRequest ( Str255 s, Str255 t, Str255 u ) { return DoBox ( requestID, s,t,u ); }
- DoAbort ( Str255 s, Str255 t, Str255 u ) { DoAlert (s,t,u); ExitToShell(); }
-
-
- // ***** High Level Apple Event ****************************************
-
- OSErr MyGotRequiredParams ( AppleEvent *theAppleEvent )
- { DescType returnedType;
- Size actualSize;
- OSErr err;
-
- err = AEGetAttributePtr ( theAppleEvent, keyMissedKeywordAttr, typeWildCard,
- &returnedType, nil, 0, &actualSize);
- if (err==errAEDescNotFound) return noErr; // you got all the required parameters
- if (err==noErr) return errAEParamMissed; // you missed a required parameter
- return err; // the call to AEGetAttributePtr failed
- }
-
- pascal OSErr HandleOpenAPP(AppleEvent *eventIn, AppleEvent *eventOut, long refCon)
- { OSErr err;
- err = MyGotRequiredParams ( eventIn );
- if (!err && !oApp) HandleINIT('OAPP'), oApp=TRUE;
- return err;
- }
-
- pascal OSErr HandleQuitAPP(AppleEvent *eventIn, AppleEvent *eventOut, long refCon)
- { OSErr err;
- err = MyGotRequiredParams ( eventIn );
- if (!err) HandleQUIT();
- return err;
- }
-
- OSErr OpenAppl (AppleEvent *eventIn, long how)
- { FSSpec file;
- AEDescList docList;
- OSErr err;
- long index, itemsInList;
- Size actualSize;
- AEKeyword keywd;
- DescType returnedType;
- err = AEGetParamDesc ( eventIn, keyDirectObject, typeAEList, &docList );
- if (err) return err;
- if (!oApp) HandleINIT('ODOC'), oApp=TRUE;
- err = MyGotRequiredParams ( eventIn );
- if (!err) err = AECountItems ( &docList, &itemsInList );
- for (index=1; index<=itemsInList && !err; index++)
- { err = AEGetNthPtr ( &docList, index, typeFSS, &keywd, &returnedType,
- (Ptr)&file, sizeof(file), &actualSize );
- if (!err) HandleOPEN ( file );
- if (!err && how=='PDOC')
- { HandlePRINT ();
- HandleCLOSE ();
- }
- }
- AEDisposeDesc(&docList);
- return err;
- }
-
- pascal OSErr HandleOpenDOC(AppleEvent *eventIn, AppleEvent *eventOut, long refCon)
- { return OpenAppl ( eventIn, 'ODOC' ); }
-
- pascal OSErr HandlePrintDOC(AppleEvent *eventIn, AppleEvent *eventOut, long refCon)
- { return OpenAppl ( eventIn, 'PDOC' ); }
-
-
- /* ***** INITIALIZATIONS **************************************** */
-
- InitToolBox()
- {
- InitGraf(&qd.thePort); InitFonts(); InitWindows();
- InitMenus(); TEInit(); InitDialogs(nil);
- InitCursor();
- }
-
-
- #if GENERATINGPOWERPC
- #define AEH(X) NewAEEventHandlerProc(X)
- #else
- #define AEH(X) X
- #endif
-
-
- InitHighLevelEvents()
- { long feature;
- if ( Gestalt(gestaltAppleEventsAttr, &feature) != noErr )
- DoAbort("\pThis program requires System 7","\p(Gestalt)","\p");
- if ( !( feature & (1L<<gestaltAppleEventsPresent) ) )
- DoAbort("\pThis program requires System 7","\p(AppleEvents)","\p");
- if ( AEInstallEventHandler ( kCoreEventClass,'oapp',AEH(HandleOpenAPP),0L,false ) == noErr )
- if ( AEInstallEventHandler ( kCoreEventClass,'odoc',AEH(HandleOpenDOC),0L,false ) == noErr )
- if ( AEInstallEventHandler ( kCoreEventClass,'pdoc',AEH(HandlePrintDOC),0L,false ) == noErr )
- if ( AEInstallEventHandler ( kCoreEventClass,'quit',AEH(HandleQuitAPP),0L,false ) == noErr )
- return;
- DoAbort("\pFailed to install an AppleEvent handler","\p","\p");
- }
-
-
- // ***** THE MAIN EVENT LOOP *******************************************
-
- main()
- { long n, selection;
- InitToolBox(); // all times needed inits
- InitHighLevelEvents(); // required apple events
- InitMenuBar(); // draw menu bar
-
- done=false;
- while (!done)
- {
- if ( WaitNextEvent(eventMask,&event,eventDelay,eventRegion) )
- {
- switch (event.what)
- {
- case kHighLevelEvent: AEProcessAppleEvent(&event);break;
- case mouseDown: HandleMouseDown(); break;
- case mouseUp: HandleMOUSEUP(); break;
- case keyUp: HandleKEYUP(event.message&0x0ff); break;
- case autoKey: HandleAUTOKEY(event.message&0x0ff); break;
- case diskEvt: HandleDISK(); break;
- case driverEvt: HandleDRIVER(); break;
- case app3Evt: HandleAPP3(); break;
- case keyDown:
- if (event.modifiers&cmdKey)
- { selection = MenuKey(event.message&0x0ff);
- if (HiWord(selection))
- { HandleMenuSelection( selection );
- break;
- }
- }
- HandleKEYDOWN (event.message&0x0ff);
- break;
- case activateEvt:
- SetPort ( (WindowPtr)event.message );
- if (event.modifiers&activeFlag)
- HandleACTIVATE (); // window activated
- else HandleDEACTIVATE (); // window deactivated
- break;
- case updateEvt:
- BeginUpdate ( (WindowPtr)event.message );
- HandleUPDATE( (WindowPtr)event.message );
- EndUpdate ( (WindowPtr)event.message );
- break;
- case osEvt:
- if (event.message & 0x01000000 )
- if ( event.message & 0x00000001 )
- SetCursor(&qd.arrow),HandleRESUME(); // application re-activated
- else HandleSUSPEND(); // application de-activated
- if ((event.message & 0xFF000000)==0xFA000000)
- HandleREGION(); // mouse leaves eventRegion
- break;
- }
- }
- else HandleNULL();
- }
- }
-
-
- HandleFileMenu(long itemNum)
- {
- if (itemNum==iNew) { HandleNEW(); return; }
- if (itemNum==iOpen)
- { StandardFileReply reply;
- StandardGetFile ( nil, oDocNumTypes, oDocTypeList, &reply );
- if (reply.sfGood) HandleOPEN ( reply.sfFile );
- return;
- }
- if (itemNum==iSave) { HandleSAVE(); return; }
- if (itemNum==iClose) { HandleCLOSE(); return; }
- if (itemNum==iQuit) { HandleQUIT(); return; }
- if (itemNum==iPrint) { HandlePRINT(); return; }
- HandleMENU ( mFile, itemNum );
- }
-
- HandleEditMenu(long itemNum)
- {
- if (itemNum==iUndo) { HandleUNDO(); return; }
- if (itemNum==iCut) { HandleCUT(); return; }
- if (itemNum==iCopy) { HandleCOPY(); return; }
- if (itemNum==iPaste) { HandlePASTE(); return; }
- HandleMENU ( mFile, itemNum );
- }
-
-
- HandleAppleMenu(long itemNum)
- { GrafPtr savePort;
- MenuHandle theMenu;
- Str255 daName;
- if (itemNum==iAbout) { Alert(aboutID,nil); return; }
- if (itemNum==iHelp) { Alert(helpID,nil); return; }
- theMenu = GetMenu ( mApple );
- if (theMenu==nil) DoAbort("\pcould not get handle to apple menu","\p","\p");
- GetItem ( theMenu, itemNum, daName );
- GetPort( &savePort );
- OpenDeskAcc ( daName );
- SetPort( savePort );
- }
-
-
- HandleMenuSelection(long menuResult)
- { long menuNum, itemNum;
- menuNum = HiWord(menuResult); if (menuNum==0) return;
- itemNum = LoWord(menuResult);
- if (menuNum==mApple) HandleAppleMenu(itemNum); else
- if (menuNum==mFile) HandleFileMenu(itemNum); else
- if (menuNum==mEdit) HandleEditMenu(itemNum); else
- HandleMENU(menuNum,itemNum);
- HiliteMenu(0);
- }
-
-
- HandleMouseDown()
- { WindowPtr window;
- long where;
- where = FindWindow ( event.where, &window );
- switch (where)
- {
- case inMenuBar: HandleMenuSelection ( MenuSelect( event.where ) ); return;
- case inDrag: DragWindow ( window, event.where, &qd.screenBits.bounds ); return;
- case inSysWindow: SystemClick ( &event, window ); return;
- case inGoAway: HandleCLOSE(); return;
- case inGrow: HandleGROW(); return;
- case inZoomIn: HandleZOOMIN(); return;
- case inZoomOut: HandleZOOMOUT(); return;
- case inContent: SelectWindow ( window ); HandleINCONTENT(); return;
- case inDesk: HandleINDESK(); return;
- }
- DoWarning ( "\pmouse down at unknown location","\p","\p" );
- }
-
-